package jukebox.controller;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import jukebox.model.member.MemberVO;
import jukebox.model.playlist.PlayListService;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class PlayListController {
	@Resource(name="playListServiceImpl")
	private PlayListService playListService;
	
	@RequestMapping("playlist_getPlayList.ju")
	public ModelAndView getPlayList(HttpSession session) {
		MemberVO mvo = (MemberVO) session.getAttribute("mvo");
		return new ModelAndView("playlist.member", "list", playListService.getPlayList(mvo.getId()));
	}
	
	@RequestMapping("playlist_ajaxPlayList.ju")
	@ResponseBody
	public List ajaxPlayList(HttpSession session) {
		MemberVO mvo = (MemberVO) session.getAttribute("mvo");
		return playListService.getPlayList(mvo.getId());
	}

	@RequestMapping("playlist_addPlayList.ju")
	public String addPlayList(String playListName, String memberId, HttpSession session) {
		MemberVO mvo = (MemberVO) session.getAttribute("mvo");
		playListService.addPlayList(playListName, memberId);
		return "redirect:playlist_getPlayList.ju?memberId="+mvo.getId();
	}

	@RequestMapping("playlist_removePlayList.ju")
	public String removePlayList(int playListNo, HttpSession session) {
		MemberVO mvo = (MemberVO) session.getAttribute("mvo");
		playListService.removePlayList(playListNo);
		return "redirect:playlist_getPlayList.ju?memberId="+mvo.getId();
	}

	@RequestMapping("playlist_uploadPlayList.ju")
	public String uploadPlayList(String playListName, int playListNo, HttpSession session) {
		MemberVO mvo = (MemberVO) session.getAttribute("mvo");
		playListService.uploadPlayList(playListName, playListNo);
		return "redirect:playlist_getPlayList.ju?memberId="+mvo.getId();
	}

	@RequestMapping("playlist_getMusicList.ju")
	public ModelAndView getMusicList(int playListNo, String playListName) {
		return new ModelAndView("musiclist.member", "list", playListService.getMusicList(playListNo));
	}

	@RequestMapping("playlist_addMusicList.ju")
	@ResponseBody
	public String addMusicList(@RequestParam Map<String, String> map) {
		return playListService.addMusicList(map);
	}

	@RequestMapping("playlist_removeMusicList.ju")
	@ResponseBody
	public String removeMusicList(int musicListNo) {
		playListService.removeMusicList(musicListNo);
		return "true";
	}
}
